package it.omnisong.server;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.queryParser.ParseException;

import it.omnisong.client.WikiSearchService;
import it.omnisong.client.model.StockArtist;
import it.omnisong.client.model.Wikilink;
import it.omnisong.server.index.WikipediaSearcher;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class WikiSearchServiceImpl extends RemoteServiceServlet implements
		WikiSearchService {

	public ArrayList<StockArtist> wikiSearch(String input) throws IllegalArgumentException {

		ArrayList<StockArtist> result = null;
		WikipediaSearcher searcher;
		try {
			result = new ArrayList<StockArtist>();
			searcher = new WikipediaSearcher();
			ArrayList<Wikilink> retrieved = searcher.searchIndex(input);
			for(Wikilink wikilink : retrieved) {
				StockArtist stock = new StockArtist(wikilink.getName(), wikilink.getUrl());
				result.add(stock);
			}
			return result;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(input);
		return(result);
	}
}
